package com.example.common.utils.result;

import lombok.Data;
import org.slf4j.MDC;

import java.io.Serializable;

/**
 * <p>
 *
 * </p>
 *
 * @author 12071
 * @date 2022/8/3 14:28
 */
@Data
public class ResultBO<T> implements Serializable {
    public static final String REQUEST_ID = "requestId";
    private static final long serialVersionUID = -5196002076749493116L;

    private Integer code;
    private Boolean success;
    private String requestId;
    private String message;
    private T data;

    public ResultBO() {
        this(ResultStatusEnum.SUCCESS);
    }

    public static final ResultBO<String> SUCCESS = new ResultBO<>();
    public static final ResultBO<String> FAIL = new ResultBO<>(ResultStatusEnum.ERROR);
    public static final ResultBO<Object> UN_AUTH = new ResultBO<>(ResultStatusEnum.ACCESS_TOKEN_NULL);
    public static final ResultBO<Object> LOGIN_EXPIRE = new ResultBO<>(ResultStatusEnum.ACCESS_TOKEN_EXPIRE);


    public ResultBO(Integer code, String message, T data, boolean success) {
        this.code = code;
        this.message = message;
        this.success = success;
        this.requestId = MDC.get(REQUEST_ID);
        this.data = data;
    }

    public ResultBO( T data) {
        this.code = ResultStatusEnum.SUCCESS.getCode();
        this.message = "";
        this.success = true;
        this.requestId = MDC.get(REQUEST_ID);
        this.data = data;
    }

    public ResultBO(ResultStatusEnum resultStatusEnum) {
        this.code = resultStatusEnum.getCode();
        this.message = resultStatusEnum.getChineseDescription();
        this.success = isSuccess();
        this.requestId = MDC.get(REQUEST_ID);
    }

    public ResultBO(ResultStatusEnum baseResultStatusEnum, T data, boolean success) {
        this(baseResultStatusEnum);
        this.data = data;
        this.success = success;
    }

    public boolean isSuccess() {
        return ResultStatusEnum.SUCCESS.getCode().equals(code);
    }

    public static <T> ResultBO<T> ok(T data) {
        return new ResultBO<>(ResultStatusEnum.SUCCESS, data, true);
    }

    public static <T> ResultBO<T> success(String message) {
        return new ResultBO<>(ResultStatusEnum.SUCCESS.getCode(), message, null, true);
    }

    public static <T> ResultBO<T> ok(String message, T data) {
        return new ResultBO<>(ResultStatusEnum.SUCCESS.getCode(), message, data, true);
    }

    public static <T> ResultBO<T> ok() {
        return new ResultBO<>(ResultStatusEnum.SUCCESS, null, true);
    }

    public static <T> ResultBO<T> error() {
        return new ResultBO<>(ResultStatusEnum.FAIL, null, false);
    }

    public static <T> ResultBO<T> error(ResultStatusEnum baseResultStatusEnum) {
        return new ResultBO<>(baseResultStatusEnum.getCode(), baseResultStatusEnum.getChineseDescription(), null, false);
    }

    public static <T> ResultBO<T> error(String message) {
        return new ResultBO<>(ResultStatusEnum.FAIL.getCode(), message, null, false);
    }

    public static <T> ResultBO<T> error(String message, T data) {
        return new ResultBO<>(ResultStatusEnum.FAIL.getCode(), message, data, false);
    }

    public static <T> ResultBO<T> error(Integer code,String message) {
        return new ResultBO<>(code, message, null, false);
    }

    public static <T> ResultBO<T> error(Integer code, String msg,T data) {
        return new ResultBO<>(code, msg, data, false);
    }

    public static <T> ResultBO<T> error(ResultStatusEnum baseResultStatusEnum, String errMsg) {
        return new ResultBO<>(baseResultStatusEnum.getCode(), errMsg, null, false);
    }

}
